<?php

/* 
 * Copyright 2015 gg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Com\Wechat\ChatService;
/**
 * 对话服务-界面丰富（自定义菜单）
 */
class MenuModel {
    /**
     * 设置微信菜单
     * @param type $token 微信access token
     * @param type $menu 需要设置的菜单内容，json字符串
     * @return blloean true：成功 false：失败
     */
    public function setWechatMenu($token,&$menu){
        $curl = new \Com\Wechat\CurlModel();
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token";
        $result = json_decode($curl->curlSafePost($url, $menu));
        if(isset($result->errcode)){
            return ($result->errcode==0);
        }else{
            return false;
        }
    }
    /**
     * 获取当前微信菜单
     * @param type $token 微信 accesstoken
     * @return false:错误 成功返回json字符串
     */
    public function getWechatMenu($token){
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$token";
        $curl = new \Com\Wechat\CurlModel();
        $result = $curl->curlSafeGet($url);
        $resultJson = json_decode(&$result);
        if(isset($resultJson->errcode)){
            return FALSE;
        }else{
            return $result;
        }
    }
    /**
     * 删除微信菜单
     * @param type $token 微信access token
     * @return type 是否成功
     */
    public function delWechatMenu($token){
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$token";
        $curl = new \Com\Wechat\CurlModel();
        $result = json_decode($curl->curlSafeGet($url));
        return $result->errcode==0;
    }
    
}